package edu.cueb.wmis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.cueb.wmis.config.JwtTokenUtil;
import edu.cueb.wmis.mapper.RoleMapper;
import edu.cueb.wmis.pojo.Admin;
import edu.cueb.wmis.mapper.AdminMapper;
import edu.cueb.wmis.pojo.RespBean;
import edu.cueb.wmis.pojo.Role;
import edu.cueb.wmis.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public String getNameById(Integer aid) {
        return adminMapper.getNameById(aid);
    }

    @Override
    public RespBean login(String username, String password) {
        //登陆
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);//该方法security自带的方法，是通过查询数据ku后得到的正确的合法用户对象UserDetails
        if(userDetails == null || !passwordEncoder.matches(password , userDetails.getPassword())){
            return RespBean.error("用户名或密码不正确");
        }
        if(!userDetails.isEnabled()){
            return RespBean.error("账号被禁用，联系管理员！");
        }
        //更新security登录用户对象
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
        //将这个认证过的token放入security的上下文中
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        //生成token
        String token = jwtTokenUtil.generatorToken(userDetails);
        Map<String , String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        return RespBean.success("登陆成功",tokenMap);
    }

    @Override
    public Admin getAdminByUserName(String username) {
        //可以再加入一些健壮性判断 此处暂且省略
        //mapper直接查询数据库 通过mybatis plus
        //selcetOne 查询一个
        //带着密码

        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("username" , username));
    }

    @Override
    public List<Role> getRoles(Integer adminID) {
        return roleMapper.getRoles(adminID);
    }
}
